// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { StandardListView } from "std-widgets.slint";

export component TestCase inherits Window {
    callback set-current-item(int);

    out property <int> count: list.model.length;
    in-out property <int> callback-current-item: -1;

    in-out property<[StandardListViewItem]> model: [
        { text: "Item 1" },
        { text: "Item 2" },
        { text: "Item 3" },
    ];
    in-out property <int> current-item <=> list.current-item;
    out property has-focus <=> list.has-focus;

    list := StandardListView {
        model: root.model;

        current-item-changed(index) => {
            root.callback-current-item = index;
        }
    }


    set-current-item(index) => {
        list.set-current-item(index);
    }
}

/*

```rust
use slint::platform::Key;
use slint::SharedString;

let instance = TestCase::new().unwrap();
assert_eq!(instance.get_count(), 3);
assert_eq!(instance.get_current_item(), -1);

assert_eq!(instance.get_callback_current_item(), -1);
instance.invoke_set_current_item(1);
assert_eq!(instance.get_callback_current_item(), 1);
assert_eq!(instance.get_current_item(), 1);

instance.set_callback_current_item(-1);
instance.invoke_set_current_item(1);
assert_eq!(instance.get_callback_current_item(), -1);
assert_eq!(instance.get_current_item(), 1);

instance.invoke_set_current_item(0);
assert_eq!(instance.get_has_focus(), false);

// Focus the listview
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Tab));
assert_eq!(instance.get_has_focus(), true);
assert_eq!(instance.get_current_item(), 0);

// Up and Down arrow keys move the selection
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
assert_eq!(instance.get_current_item(), 0);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
assert_eq!(instance.get_current_item(), 1);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
assert_eq!(instance.get_current_item(), 2);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
assert_eq!(instance.get_current_item(), 2);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
assert_eq!(instance.get_current_item(), 1);

// Pressing the Home key selects the first item
instance.invoke_set_current_item(2);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Home));
assert_eq!(instance.get_current_item(), 0);

// Pressing the End key selects the last item
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::End));
assert_eq!(instance.get_current_item(), 2);

// Pressing the space bar on the selected item does not deselect it
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), 2);

// Control+Space deselects the currently focused item
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), -1);

// Control+Space selects the currently focused item
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), 2);

// Control+UpArrow moves the focus up one item but not the selection
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
assert_eq!(instance.get_current_item(), 2);

// Pressing the space bar selects the currently focused item if it is not
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), 1);

// Control+DownArrow moves the focus down one item but not the selection
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
assert_eq!(instance.get_current_item(), 1);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), 2);

// Control+Home moves the focus to the first item but not the selection
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Home));
assert_eq!(instance.get_current_item(), 2);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), 0);

// Control+End moves the focus to the last item but not the selection
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::End));
assert_eq!(instance.get_current_item(), 0);
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
assert_eq!(instance.get_current_item(), 2);

// Invoking the accessible default action of an item selects it
let mut item2_search = slint_testing::ElementHandle::find_by_accessible_label(&instance, "Item 2");
let item2 = item2_search.next().unwrap();
item2.invoke_accessible_default_action();
assert_eq!(instance.get_current_item(), 1);
```

*/
